#!/usr/bin/python
# -*- coding: utf-8 -*-

import os
import sys
import petlib
import Exceptions

global pet
global developer

def updatesvn_():
    if len(sys.argv) >= 3:
        for repo in sys.argv[2:]:
            pet.updatesvn(repo)
    else:
        pet.updatesvn()

def listpackages_():
    for package in developer.getPackages():
        print package.packagename

def set_():

    if len(sys.argv) <= 2:
        print
        print " You must give an attribute name to set."
        print " These attributes are avaliable :"
        for att in petlib.default_attr:
            print "  %s " % att
        print
        return

    if sys.argv[2] in petlib.default_attr:
        if len(sys.argv) <= 3:
            set_to = raw_input("Set to what ? ")
        else:
            set_to = sys.argv[3]
        setattr(developer, sys.argv[2], set_to)
    developer.saveDeveloperData(pet.config_path)

def setupuser_():
    if len(sys.argv) <= 2:
        newuser = raw_input("Enter user name : ")
    else:
        newuser = sys.argv[2]
    pet = petlib.Pet(newuser)
    sys.exit()

def add_():
    pass

def check_():
    pass

def checkall_():
    pass

def showusage():
    print " Avaliable commands are "
    print
    for command in commands.keys():
        print " %*s : %s" % (14,command, commands[command][1])
    print
    sys.exit(0)

commands = {
        "updatesvn"     :[updatesvn_,    "It updates defined SVN repositories."],
        "listpackages"  :[listpackages_, "It shows your packages."],
        "add"           :[add_,          "It adds given package to the list."],
        "set"           :[set_,          "It sets given value to given attribute."],
        "help"          :[showusage,     "It shows this screen."],
        "check"         :[check_,        "It checks given package changes."],
        "setupuser"     :[setupuser_,    "It will create a new config for given username."],
        "checkall"      :[checkall_,     "It checks all packages in your list."]
    }

if __name__ == "__main__":

    if len(sys.argv) == 1:
        print "No command given."
        sys.exit(0)

    if sys.argv[1] not in commands.keys() or sys.argv[1] == "help":
        print
        print " Unrecognized command:", sys.argv[1]
        showusage()

    try:
        pet = petlib.Pet()
    except Exceptions.NotFound, e:
        if sys.argv[1] == "setupuser":
            setupuser_()
        else:
            print e
            sys.exit()

    developer = pet.developer
    commands[sys.argv[1]][0]()

"""
pet = petlib.Pet("gokmen")
repo = petlib.Developer.Repo("pardus-2008")
package = petlib.Developer.Package("python")
package.homepage = "http://www.python.org"
repo.addPackage(package)
pet.developer.addRepo(repo)
pet.developer.saveDeveloperData(pet.config_path)
repo = petlib.Developer.Repo("devel")
package = petlib.Developer.Package("yali4")
package.dependencies = "python"
repo.addPackage(package)
pet.developer.addRepo(repo)
pet.developer.saveDeveloperData(pet.config_path)
pet.developer.email="gokmen@pardus.org.tr"
pet.developer.saveDeveloperData(pet.config_path)
"""

